
var express = require('express');

var app = express();

var secret = require('./config/credentials');

var logger = require('morgan');
var fs = require('fs')

var accessLogStream = fs.createWriteStream(__dirname + '/access.log',{flags: 'a'});

// app.use(logger('dev'));     //打印到控制台  
app.use(logger('combined', {stream : accessLogStream}));      //打印到log日志  

app.use(require('cookie-parser')(secret.salt))

const session = require('express-session')
app.use(session({
    secret: 'zhouABC'
    // resave: true,
    // aveUninitialized: true
}))

app.use('/public',express.static(__dirname + '/public'));
app.use('/views',express.static(__dirname + '/views'));

app.use(function(req,res,next){

    console.log('****');
    for(var name in req.headers){
        console.log(name,'=',req.headers[name]);
    }
    next();
    console.log('####');
})
app.get('/',function(req,res){

    req.session.userName = 'zhou';
    res.cookie('zhou','nom nom',{httpOnly: true});
    res.cookie('wng',12333);
    res.cookie('li','123456789')
    return res.redirect(303,'/views/fileHome.html');
});

app.use('/files',require('./routes/fileRoute'));

// app.use('/musics',require('./routes/musicRoute'));


//全局错误处理
app.use(function (err, req, res, next) {
    res.status(err.status || 500).json({
        code: -1,
        msg: 'server err'
    })
})

var conf = require('./tool/configTool');
const port = conf.sys.server.port || 9000;

app.listen(port,function(){

    console.log("App listening on port",port);
})



const mongoose = require('mongoose');

mongoose.connect(secret.mongo.connectStr,{useNewUrlParser:true});
const con = mongoose.connection;
con.on('error', console.error.bind(console, '连接数据库失败'));
con.once('open',()=>{
    //成功连接
    console.log('mongo connect ok')
})

var color = require('./tool/colorTool')
// console.log(' please visit:   http://localhost:9000')
color.GREEN.print(' please visit:   http://localhost:'+port)